package com.sleepan.admin.util;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

public class HttpHelper {
    private CloseableHttpClient httpclient;

    public HttpHelper() {
        httpclient = HttpClients.createDefault();
    }

    public String postBackString(String url, String param, Map<String, String> headers) throws IOException {
        HttpResponse httpResponse = getHttpResponse(url, param, headers);
        return EntityUtils.toString(httpResponse.getEntity());
    }

    public byte[] postBackByte(String url, String param, Map<String, String> headers) throws IOException {
        HttpResponse httpResponse = getHttpResponse(url, param, headers);
        return EntityUtils.toByteArray(httpResponse.getEntity());
    }

    public byte[] getImageByUrl(String url) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpGet);
        return EntityUtils.toByteArray(response.getEntity());
    }

    public String postFile(String url, byte[] bytes, String fileName) {
        CloseableHttpClient client = null;
        String result = null;
        try {
            client = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("form", bytes, ContentType.MULTIPART_FORM_DATA, fileName);
            httpPost.setEntity(builder.build());
            HttpResponse response = client.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == 200) {
                // 取回服务器端的响应结果
                result = EntityUtils.toString(response.getEntity(), "utf-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    private HttpResponse getHttpResponse(String url, String param, Map<String, String> headers) throws IOException {
        HttpPost httpPost = new HttpPost(url);
        HttpEntity httpEntity = new StringEntity(param, ContentType.APPLICATION_JSON);
        httpPost.setEntity(httpEntity);
        Header[] header = getHeaders(headers);
        httpPost.setHeaders(header);
        return httpclient.execute(httpPost);
    }


    public String get(String url) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpclient.execute(httpGet);
        return EntityUtils.toString(httpResponse.getEntity());
    }

    public String getWithHeader(String url,Map<String, String> headers) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        Header[] header = getHeaders(headers);
        httpGet.setHeaders(header);
        HttpResponse httpResponse = httpclient.execute(httpGet);
        return EntityUtils.toString(httpResponse.getEntity());
    }

    private Header[] getHeaders(Map<String, String> headers) {
        Header[] header = null;
        if (headers != null) {
            header = new Header[headers.size()];
            int index = 0;
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                header[index++] = new BasicHeader(entry.getKey(), entry.getValue());
            }
        }
        return header;
    }

}
